//
//  ZYTokenManager.m
//  自定义搜索框并缓存搜索记录
//
//  Created by xiayong on 16/3/11.
//  Copyright © 2016年 bianguo. All rights reserved.
//

#import "HJTTokenManager.h"

@implementation HJTTokenManager


//缓存搜索数组
+(void)SearchText :(NSString *)seaTxt
{
    NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
    NSMutableArray *myArray = (NSMutableArray * )[userDefaultes arrayForKey:@"myArray"];
    
    for (NSString *str in myArray) {
        if ([str isEqualToString:seaTxt]) {
            return;
        }
    }
    
    if (myArray.count > 0) {
    }else{
        myArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    NSMutableArray *searTXT = [myArray mutableCopy];
    [searTXT addObject:seaTxt];
    if(searTXT.count > 15)
    {
        [searTXT removeObjectAtIndex:0];
    }
    [userDefaultes setObject:searTXT forKey:@"myArray"];
    [userDefaultes synchronize];
}
+(void)removeAllArray{
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults removeObjectForKey:@"myArray"];
    [userDefaults synchronize];
}

+(void)removeArrayObjectAtIndex:(NSInteger)index{
    NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
    NSMutableArray *myArray = (NSMutableArray * )[userDefaultes arrayForKey:@"myArray"];
    if (!myArray.count || myArray.count < index + 1) {
        return;
    }
    myArray = (NSMutableArray *)[[myArray reverseObjectEnumerator] allObjects];
    [myArray removeObjectAtIndex:index];
    
    NSMutableArray *newArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (NSInteger i = (myArray.count - 1); i >= 0 ; i --) {
        [newArray addObject:myArray[i]];
        
    }
    [userDefaultes setObject:newArray forKey:@"myArray"];
    [userDefaultes synchronize];
}

@end
// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com